//
//  SendCodeButton.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/12.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "SendCodeButton.h"


static NSInteger timeCount = 60;

@interface SendCodeButton()
@property (nonatomic, strong) RACDisposable *dispoable;
@end

@implementation SendCodeButton



- (void)initialTime{
    @weakify(self)
    [[self rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        x.enabled = false;
        //这个就是RAC中的GCD
        self.dispoable = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {
            timeCount --;
            NSString * title = timeCount > 0 ? [NSString stringWithFormat:@" %lds",timeCount] : @"发送验证码";
            [self setTitleColor:timeCount > 0 ? KLColor_B4:KLColor_A1 forState:UIControlStateNormal];
            [self setTitle:title forState:UIControlStateNormal | UIControlStateDisabled];
            self.enabled = (timeCount==0)? YES : NO;
            if (timeCount == 0) {
                [self.dispoable dispose];
            }
        }];
    }];

}

@end
